/**
 * Created by sofwu on 2016/12/22 0022.
 */
import logger from '../utils/logger'

export function getAccessToken() {
    return global.accessToken;
}


export function setAccessToken(accessToken) {
    global.accessToken = accessToken;
}


export function getAuthToken() {
    return global.authToken;
    // return null;
}


export function setAuthToken(authToken) {
    global.authToken = authToken;
}

export function remAuthToken() {
  global.authToken = null
}


export function getUserId() {
  return global.userId;
  // return 2783;
}


export function setUserId(userId) {
  global.userId = userId;
}

export function remUserId() {
  global.userId = null
}

//是否登录
export function isLoggedIn(){
	const authToken = getAuthToken();

	logger.info('auth token:', authToken);
	if(authToken) return true;
	else return false;
}

// //登录系统
export function login(loginState){
	if(!loginState.data.token){
		logger.warn('Login Token Error!')
		return false
	}

	let expires = 1000 * 3600 * 24 * 30
  //保存token信息到storage中
  storage.save({
    key: 'loginState',
    data: loginState.data,
    expires: expires
  });
  //保存token到内存中
  setAuthToken(loginState.data.token);
  setUserId(loginState.data.user_id);
  logger.info('Login Success', loginState)
  return true
}

//注销系统
export function logout(){
  storage.remove({
    key: 'loginState'
  });
  remAuthToken()
  return true
}